package com.wind.taobao.api.trade.traderate.convert
{
	import com.wind.taobao.api.trade.traderate.model.TradeRate;
	import com.wind.taobao.core.util.TaobaoConstants;

	import mx.collections.ArrayCollection;


	/**
	 * TradeRate转换器
	 * @author fxy
	 *
	 */
	public class TradeRateConvert
	{
		/**
		 * 转换TradeRate集合
		 */
		public static function convertTradeRates(value:Object, type:String="json"):ArrayCollection
		{
			var tradeRates:ArrayCollection=new ArrayCollection();
			if (value != null)
			{
				if (type == TaobaoConstants.JSON)
				{
					if ((value as Array).length > 0)
					{
						for (var i:uint=0; i < (value as Array).length; i++)
						{
							tradeRates.addItem(convertTradeRate((value as Array)[i]));
						}
					}
				}
				else
				{
					//TODO
				}
			}
			return tradeRates;
		}

		/**
		 * 转换单个TradeRate对象
		 * value:XML 或 JSON
		 */
		public static function convertTradeRate(value:Object, type:String="json", tradeRate:TradeRate=null):TradeRate
		{
			if (tradeRate == null)
				tradeRate=new TradeRate();
			if (value == null)
				return tradeRate;
			if (type == TaobaoConstants.XML)
			{
				tradeRate=convertXMLToTradeRate(XML(value), tradeRate);
			}
			else
			{
				tradeRate=convertJSONToTradeRate(value, tradeRate);
			}
			return tradeRate;
		}

		/**
		 * JSON转换为TradeRate
		 */
		private static function convertJSONToTradeRate(json:Object, tradeRate:TradeRate):TradeRate
		{
			if (json.hasOwnProperty(TaobaoConstants.TID))
			{
				tradeRate.tid=json[TaobaoConstants.TID];
			}

			if (json.hasOwnProperty(TaobaoConstants.ORDER_ID))
			{
				tradeRate.order_id=json[TaobaoConstants.ORDER_ID];
			}
			if (json.hasOwnProperty(TaobaoConstants.ROLE))
			{
				tradeRate.role=json[TaobaoConstants.ROLE];
			}
			if (json.hasOwnProperty(TaobaoConstants.NICK))
			{
				tradeRate.nick=json[TaobaoConstants.NICK];
			}
			if (json.hasOwnProperty(TaobaoConstants.RESULT))
			{
				tradeRate.result=json[TaobaoConstants.RESULT];
			}

			if (json.hasOwnProperty(TaobaoConstants.CREATED))
			{
				tradeRate.created=json[TaobaoConstants.CREATED];
			}
			if (json.hasOwnProperty(TaobaoConstants.RATED_NICK))
			{
				tradeRate.rated_nick=json[TaobaoConstants.RATED_NICK];
			}
			if (json.hasOwnProperty(TaobaoConstants.ITEM_TITLE))
			{
				tradeRate.item_title=json[TaobaoConstants.ITEM_TITLE];
			}
			if (json.hasOwnProperty(TaobaoConstants.ITEM_PRICE))
			{
				tradeRate.item_price=json[TaobaoConstants.ITEM_PRICE];
			}
			if (json.hasOwnProperty(TaobaoConstants.CONTENT))
			{
				tradeRate.content=json[TaobaoConstants.CONTENT];
			}
			if (json.hasOwnProperty(TaobaoConstants.REPLY))
			{
				tradeRate.reply=json[TaobaoConstants.REPLY];
			}

			return tradeRate;
		}



		/**
		 * XML转换为TradeRate
		 */
		private static function convertXMLToTradeRate(value:XML, tradeRate:TradeRate):TradeRate
		{
			return tradeRate;
		}



	}
}